package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeacherDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.MoveTeachplan;
import com.xuecheng.content.model.po.Teachers;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description TODO
 * @date 2023/2/14 12:11
 */
@Slf4j
@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        List<TeachplanDto> teachplanDtos = teachplanMapper.selectTreeNodes(courseId);
        return teachplanDtos;
    }

    private int getTeachplanCount(Long courseId,Long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return  count+1;
    }
    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        //通过课程计划id判断是新增和修改
        Long teachplanId = saveTeachplanDto.getId();
        if(teachplanId ==null){
            //新增
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            //确定排序字段，找到它的同级节点个数，排序字段就是个数加1  select count(1) from teachplan where course_id=117 and parentid=268
            Long parentid = saveTeachplanDto.getParentid();
            Long courseId = saveTeachplanDto.getCourseId();
            int teachplanCount = getTeachplanCount(courseId, parentid);
            teachplan.setOrderby(teachplanCount);
            teachplanMapper.insert(teachplan);

        }else{
            //修改
            Teachplan teachplan = teachplanMapper.selectById(teachplanId);
            //将参数复制到teachplan
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }

    }

    @Transactional
    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //课程计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan == null){
            XueChengPlusException.cast("课程计划不存在");
        }

        //先删除原有记录,根据课程计划id删除它所绑定的媒资
        int delete = teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, bindTeachplanMediaDto.getTeachplanId()));

        //再添加新记录
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMediaMapper.insert(teachplanMedia);

    }

    @Override
    public Integer deleteCouse(Long id) {
        //检查该节课程下是否有细分
        Long t = teachplanMapper.countById(id);
        int count= Math.toIntExact(t);
        if(count>0){
            return 0;
        } else {
            teachplanMapper.deleteTeachplan(id);
            teachplanMediaMapper.deleteTeachplanMedia(id);
        }
        return 1;
    }

    /**
     * 课程计划下移操作
     * @param id
     */
    @Override
    public void modedownTeachplan(Long id) {
        //判断是否时一个章节还是一个小节
        MoveTeachplan moveTeachplan=teachplanMapper.selectMoveIds(id);
        Long parentId = moveTeachplan.getParentId();
        Long courseId = moveTeachplan.getCountId();
        if(parentId==0){
            //查询章节数量，只有一个章节则无法向下移动
            Long count = teachplanMapper.counts(courseId);
            if(count<=1){
                return;
            } else {
                Long orderBy=moveTeachplan.getOrderBy();
                System.out.println("序号"+orderBy);
                if(orderBy==count){
                    Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndCourseId(1L,courseId);
                    teachplanMapper.updateTeachPlanOrderBy(id,1L);
                    teachplanMapper.updateTeachPlanOrderBy(teachPlanId,count);
                    return;
                }
                Long newOrderBy2=orderBy+1;
                Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndCourseId(newOrderBy2,courseId);
                teachplanMapper.updateTeachPlanOrderBy(id,newOrderBy2);
                teachplanMapper.updateTeachPlanOrderBy(teachPlanId,orderBy);
            }
        } else {
            Long smallcounts=teachplanMapper.smallcounts(parentId);
            if(smallcounts<=1){
                return;
            } else {
                Long orderBy=moveTeachplan.getOrderBy();
                System.out.println("序号"+orderBy);
                if(orderBy==smallcounts){
                    Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndParentId(1L,parentId);
                    teachplanMapper.updateTeachPlanOrderBy(id,1L);
                    teachplanMapper.updateTeachPlanOrderBy(teachPlanId,smallcounts);
                    return;
                }
                Long newOrderBy4=orderBy+1;
                Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndParentId(newOrderBy4,parentId);
                teachplanMapper.updateTeachPlanOrderBy(id,newOrderBy4);
                teachplanMapper.updateTeachPlanOrderBy(teachPlanId,orderBy);
            }
        }
    }

    @Override
    public void moveupTeachplan(Long id) {
        //判断是否时一个章节还是一个小节
        MoveTeachplan moveTeachplan=teachplanMapper.selectMoveIds(id);
        Long parentId = moveTeachplan.getParentId();
        Long courseId = moveTeachplan.getCountId();
        if(parentId==0){
            //查询章节数量，只有一个章节则无法向上移动
            Long count = teachplanMapper.counts(courseId);
            if(count<=1){
                return;
            } else {
                Long orderBy=moveTeachplan.getOrderBy();
                System.out.println("序号"+orderBy);
                if(orderBy==1L){
                    Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndCourseId(count,courseId);
                    teachplanMapper.updateTeachPlanOrderBy(id,count);
                    teachplanMapper.updateTeachPlanOrderBy(teachPlanId,1L);
                    return;
                }
                Long newOrderBy2=orderBy-1;
                Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndCourseId(newOrderBy2,courseId);
                teachplanMapper.updateTeachPlanOrderBy(id,newOrderBy2);
                teachplanMapper.updateTeachPlanOrderBy(teachPlanId,orderBy);
            }
        } else {
            Long smallcounts=teachplanMapper.smallcounts(parentId);
            if(smallcounts<=1){
                return;
            } else {
                Long orderBy=moveTeachplan.getOrderBy();
                System.out.println("序号"+orderBy);
                if(orderBy==1L){
                    Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndParentId(smallcounts,parentId);
                    teachplanMapper.updateTeachPlanOrderBy(id,smallcounts);
                    teachplanMapper.updateTeachPlanOrderBy(teachPlanId,1L);
                    return;
                }
                Long newOrderBy4=orderBy-1;
                Long teachPlanId=teachplanMapper.teachPlanIdByOrderByAndParentId(newOrderBy4,parentId);
                teachplanMapper.updateTeachPlanOrderBy(id,newOrderBy4);
                teachplanMapper.updateTeachPlanOrderBy(teachPlanId,orderBy);
            }
        }
    }

    @Override
    public List<Teachers> getTeachersByCourseId(Long id) {
        List<Teachers> teachersList = teachplanMapper.getTeachersByCourseId(id);
        return teachersList;
    }

    @Override
    public Long saveTeacher(TeacherDto teacherDto) {
        Teachers teachers=new Teachers();
        BeanUtils.copyProperties(teacherDto,teachers);
        teachplanMapper.saveTeacher(teachers);
        Long id=teachers.getId();
        return id;
    }

    @Override
    public void updateTeacher(Teachers teachers) {
        teachplanMapper.updateTeacher(teachers);
    }

    @Override
    public void deleteTeacher(Long courseId, Long teacherId) {
        teachplanMapper.deleteTeacher(courseId,teacherId);
    }

    @Override
    public void deleteTeachPlan(Long courseId) {
        teachplanMapper.deleteCourseBase(courseId);
        teachplanMapper.deleteCourseMarket(courseId);
        teachplanMapper.deleteTeachPlanCourse(courseId);
        teachplanMapper.deleteTeachPlanMedia(courseId);
        teachplanMapper.deleteTeachWork(courseId);
        teachplanMapper.deleteTeacherByCourseId(courseId);
    }


}
